require.config({baseUrl: '/js/lib', urlArgs: "v0.0.1"});
require(["zepto", "lib", 'swiper'], function ($, lib, swiper) {
    lib = new lib();
    var fn = {};
    $.extend(fn, {
        timeInterVal: null,
        init: function () {
            this.initPage();
            this.bindEvent();
        },
        bindEvent: function () {
            var _t = this;
            /*监测输入框*/
            $("input[name=userTel]").keyup(function () {
                if ($(this).val().trim().length > 0 && !$(".ui-send-code").hasClass('ui-ing')) {
                    $(".ui-send-code").removeClass("ui-time-show");
                } else {
                    $(".ui-send-code").addClass("ui-time-show");
                }
            });
            $("input[name=userCode]").keyup(function () {
                if ($(this).val().trim().length > 0 && !$(".ui-red-botton").hasClass('ui-ing')) {
                    $(".ui-red-botton").removeClass("ui-disable");
                } else {
                    $(".ui-red-botton").addClass("ui-disable");
                }
            });
            /*点击发送验证码*/
            $(document).on("tap", ".ui-send-code:not(.ui-time-show)", function () {
                var _this = this;
                $(_this).addClass("ui-ing ui-time-show");
                if (!_t.checkTel($("input[name=userTel]").val())) {
                    lib.showMessage("请输入正确的手机号");
                    $(_this).removeClass("ui-ing ui-time-show");
                    return;
                }
                lib.ajx(lib.getReq().ser + '/verifyCode/sendSmsCode.action', {phone: $("input[name=userTel]").val()}, function (data) {
                    lib.showMessage(data.info);
                    var time = 120;
                    _t.timeInterVal = null;
                    _t.timeInterVal = setInterval(function () {
                        if (--time < 1) {
                            $(_this).text('发送验证码');
                            $(_this).removeClass("ui-ing ui-time-show");
                            clearInterval(_t.timeInterVal);
                            return;
                        }
                        $(_this).text(time + 's"');
                    }, 1000);

                }, function () {
                    lib.showMessage("网络超时，请稍后再试");
                    $(_this).removeClass("ui-ing ui-time-show");
                });
            });

            /*点击登陆按钮*/
            $(document).on("tap", ".ui-red-botton:not(.ui-disable)", function (data) {
                var _this = this;
                $(_this).addClass("ui-ing ui-disable");
                if (!_t.checkTel($("input[name=userTel]").val())) {
                    lib.showMessage("请输入正确的手机号");
                    $(_this).removeClass("ui-ing ui-disable");
                    return;
                }
                if ($("input[name=userCode]").val().trim().length === 0) {
                    lib.showMessage("请输入短信验证码");
                    $(_this).removeClass("ui-ing ui-disable");
                    return;
                }
                lib.onLoading();
                lib.ajx(lib.getReq().ser + 'classroom/apply.action', {
                    mobile: $("input[name=userTel]").val(),
                    smsCode: $("input[name=userCode]").val(),
                    classroomId: lib.getUrlParam('classid')
                }, function (data) {
                    if (data.infocode === "0") {
                        lib.showMessage("报名成功！");
                        setTimeout(function(){
                            location.href = '/school/index.html';
                        },1500);
                    } else {
                        lib.showMessage(data.info);
                        $(_this).removeClass("ui-ing ui-disable");
                    }

                }, function () {
                    lib.showMessage("网络超时，请稍后再试");
                    $(_this).removeClass("ui-ing ui-disable");
                });
            });
        },
        checkTel: function (num) {
            var numReg = /^1[34578]\d{9}$/;
            return numReg.test(num);
        },
        initPage: function () {
            var _t = this;
            var classid = lib.getUrlParam('classid') ? lib.getUrlParam('classid') : '1';
            lib.ajx(lib.getReq().ser + 'classroom/getTip.action', {classroomId: classid}, function (data) {
                if (data.infocode === "0") {
                    $('#signup_tip').html(data.info);
                } else {
                    lib.showMessage(data.info);
                }
            }, function () {
                lib.showMessage('网络延时，请稍后再试');
            });
        }
    });
    fn.init();
});
